


package yf.mall.controller;
import com.yf.request.SearchParam;

import java.util.ArrayList;
import java.util.List;

import com.yf.response.RRException;
import com.yf.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.dao.MallGoodsDao;
import yf.mall.entity.DocDownEntity;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.enums.MallGoodsShelfEnum;
import yf.mall.service.MallGoodsService;
import yf.mall.service.SysUsersService;
import yf.mall.vo.DocDownVo;
import yf.mall.service.DocDownService;
import com.yf.response.R;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
                                import yf.mall.enums.DocDownStatusEnum;
    
/**
 * 文档下载Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Dec 22 10:37:47 CST 2020
 */
@RestController
@RequestMapping("/doc/down")
public class DocDownController {
    @Autowired
    private DocDownService docDownService;
    @Autowired
    private MallGoodsDao mallGoodsDao;
    @Autowired
    private SysUsersService sysUsersService;
    /**
     * 列表文档下载
     */
    @SysLog("查看文档下载")
    @RequestMapping("/list")
    @RequiresPermissions("doc:down:list")
    public R list(@RequestBody SearchParam<DocDownVo> params) {
        Long sysUserId = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sysUserId);
        params.getEntity().setUserId(userId);
        List<MallGoodsEntity> goods = mallGoodsDao.findByUserId(userId,null);
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<DocDownVo> list = docDownService.findVo(params.getEntity());
        return R.ok(new PageInfo(list)).putExtra("goods",goods).putExtra("userId",userId);
    }

    /**
 * 下载订单
 */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("doc:down:list")
    public void down(@RequestBody SearchParam<DocDownVo> params, HttpServletResponse response) {
        List<DocDownVo> list = docDownService.findVo(params.getEntity());
            docDownService.downLoad(response, list);
    }


    /**
     * 查看文档下载
     */
    @SysLog("查看文档下载")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("doc:down:info")
    public R info(@PathVariable("id") Long id) {
            DocDownVo vo = docDownService.getVo(id);

        return R.ok(vo);
    }

    /**
     * 保存文档下载
     */
    @SysLog("保存文档下载")
    @RequestMapping("/save")
    @RequiresPermissions("doc:down:save")
    public R save(@RequestBody DocDownVo vo) {
        before(vo);
        docDownService.saveVo(vo);
        return R.ok(vo);
    }

    /**
     * 修改文档下载
     */
    @SysLog("修改文档下载")
    @RequestMapping("/update")
    @RequiresPermissions("doc:down:update")
    public R update(@RequestBody DocDownVo vo) {
        before(vo);
        docDownService.updateVo(vo);
        return R.ok(vo);
    }

    private void before(DocDownVo vo) {
        MallGoodsEntity mallGoodsEntity = mallGoodsDao.get(vo.getDocDown().getGoodsId());
        if (mallGoodsEntity == null){
            throw new RRException("产品不存在");
        }
        List<DocDownEntity> docDownEntities = docDownService.queryByGoodsId(mallGoodsEntity.getId(), null);
        if (vo.getDocDown().getId() == null && docDownEntities.size() == 5){
            throw new RRException("同个产品最多只能上传5个文件");
        }
        Long sysUserId = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sysUserId);
        if (userId != null){
            vo.getEntity().setStatus(DocDownStatusEnum.AUDIT.getIndex());
        }
        vo.getDocDown().setGoodsName(mallGoodsEntity.getName());
    }

    /**
     * 删除文档下载
     */
    @SysLog("删除文档下载")
    @RequestMapping("/delete")
    @RequiresPermissions("doc:down:delete")
    public R delete(Long[]ids) {
            docDownService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有文档下载
     */
    @SysLog("所有文档下载")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order!=null ? order : "id desc");
        List<DocDownVo> list = docDownService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String,Object> options = new HashMap();
                                                                                                                                        options.put("statusOptions",DocDownStatusEnum.names());
                            return R.ok(options);
    }





    /***********自定义开始***********/
}
